package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/23 11:35
 */
@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public PageInfo getByConditions(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Menu> menuList= menuDao.getByConditions(queryPageBean.getQueryString());
        PageInfo pageInfo = new PageInfo(menuList);
        return pageInfo;
    }

    @Override
    @Transactional
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    @Override
    @Transactional
    public void update(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public Menu getById(Integer id) {
        Menu menu = menuDao.getById(id);
        return menu;
    }

    /*
    * {
        "path": "2",
        "title": "会员管理",
        "icon":"fa-user-md",
        "children": [
            {
                "path": "/2-1",
                "title": "会员档案",
                "linkUrl":"member.html",
                "children":[]
            },
            {
                "path": "/2-2",
                "title": "体检上传",
                "children":[]
            }
        ]
    },
    * */
    @Override
    public List<Map> chageMenu() {
        List<Menu> menuList=menuDao.chageMenu();
        //对拿到的数据进行处理
        List<Map> list = new ArrayList<>();
        //1、先将一级菜单放至好
        for (Menu menu : menuList) {
            if (menu.getParentMenuId() == null) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("path", menu.getPath());
                map.put("title", menu.getName());
                map.put("icon", menu.getIcon());
                map.put("children",new ArrayList<Map>());
                list.add(map);
            }
        }
        //把二级菜单放至到一级菜单中
        for (Menu menu : menuList) {
            if (menu.getParentMenuId() != null) {
                for (Map map : list) {
                    if (map.get("id") == menu.getParentMenuId()) {
                        List children = (List) map.get("children");
                        Map<String, Object> mapSon = new HashMap<>();
                        mapSon.put("id", menu.getId());
                        mapSon.put("path", menu.getPath());
                        mapSon.put("title", menu.getName());
                        mapSon.put("linkUrl", menu.getLinkUrl());
                        mapSon.put("children",new ArrayList<Map>());
                        children.add(mapSon);
                    }
                }
            }
        }
        //通过路径进行排序,冒泡排序
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = 0; j < list.size()-i-1; j++) {
                Map map1 = list.get(j);
                String path1 = (String) map1.get("path");
                int a1 = Integer.parseInt(path1);
                Map map2 = list.get(j+1);
                String path2 = (String) map2.get("path");
                int a2 = Integer.parseInt(path2);
                Map<String, Object> map3 = new HashMap<>();
                if (a1 > a2) {
                    map1=map3;
                    map1=map2;
                    map2=map3;
                }
            }
        }
        return list;
    }


}
